package cn.wsn2018.tools.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

/**
 * @author gmz.
 * @date 2018/12/14.
 * @time 19:35
 */
public  class DateUtil {
    SimpleDateFormat simpleDateFormat;

    public String dateToString(Date date){
        simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        simpleDateFormat.setCalendar(new GregorianCalendar(new SimpleTimeZone(1000*60*60*8,"GMT")));
        String ctime = simpleDateFormat.format(date);
        return ctime;
    }

    public Date StringToDate(String dateString){
        simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        simpleDateFormat.setCalendar(new GregorianCalendar(new SimpleTimeZone(1000*60*60*8,"GMT")));
        Date date = null;
        try{
            date = simpleDateFormat.parse(dateString);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date ;
    }

    /**
     * 判断X时间是否在该合理时间范围内
     * @param fromTime
     * @param toTime
     * @return
     */
    public boolean JudgeTimeRange(String fromTime,String toTime,String time){
        int res1 = time.compareTo(fromTime);
        int res2 = time.compareTo(toTime);
        if(res1 >= 0 && res2 <= 0){
            return true;
        }
        else{
            return false;
        }
    }

//    public static String dateFormateToString(String timeStr,String formatterStr){
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatterStr);
//        LocalDate localDate = LocalDate.parse(timeStr, dateTimeFormatter);
//        return localDate.toString().replace("-","/");
//    }

}
